/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.ProfileDao;
import com.tesam.gedeon.entity.Profile;
import com.tesam.gedeon.service.ProfileService;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author joelpacheco
 */
@Service
public class ProfileServiceImpl implements ProfileService {

    @Autowired
    private ProfileDao profileDao;

    @Override
    public int save(Profile profile) {
        return profileDao.save(profile);
    }

    @Override
    public int delete(int id) {
        return profileDao.delete(id);
    }

    @Override
    public int update(Profile profile) {
        return profileDao.update(profile);
    }

    @Override
    public Profile getById(int id) {
        return profileDao.getById(id);
    }

    @Override
    public List<Profile> getAll() {
        return profileDao.getAll();
    }

    @Override
    public List<Profile> getAllByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return profileDao.getAllByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Profile> searchByName(Profile profile, int satartLimit, int endLimit) {
        return profileDao.searchByName(profile, satartLimit, endLimit);
    }

    @Override
    public int getCountSearchByName(Profile rol) {
        return profileDao.getCountSearchByName(rol);
    }

    @Override
    public int getCountTotal() {
        return profileDao.getCountTotal();
    }

    @Override
    public String getJsonDataFromArrayList(List<Profile> listaDeProfile, int cantidadRegistros) {
        JSONArray array = null;
        JSONObject json = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listaDeProfile.size() > 0) {

                for (Profile profile : listaDeProfile) {
                    JSONArray arrayPut = new JSONArray();

                    arrayPut.put(profile.getId());
                    arrayPut.put(StringEscapeUtils.escapeHtml4(profile.getName()));
                    arrayPut.put(profile.getDescription());
                    arrayPut.put(profile.getDateCreated());
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }

    @Override
    public List<Profile> getAllByRolId(int rolId) {
       return profileDao.getAllByRolId(rolId);
    }
}
